package cn.zjjd.homeworkserver.controller;

import cn.zjjd.homeworkserver.pojo.Device;
import cn.zjjd.homeworkserver.util.IotSdkClient;
import cn.zjjd.homeworkserver.util.ReturnMessage;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 小胡
 * @create: 2022/12/19
 * @Description:
 * @FileName: testController
 * @Version: 1.0
 */
@RestController
public class MainController {
    @GetMapping("/test")
    public void sendMessage(){
        IotSdkClient.createIotSdkClient();
    }
    @PostMapping("/adddevice")
    public ReturnMessage addDevice(String deviceName,String nickName){
        ReturnMessage rm = new ReturnMessage();
        try{
            IotSdkClient.addDevice(deviceName,nickName);
            rm.setSuccess(true);
            return rm;
        }catch (Exception e){
            rm.setSuccess(false);
            return rm;
        }
    }
    @PostMapping("/deletedevice")
    public ReturnMessage deleteDevice(String deviceName){
        ReturnMessage rm = new ReturnMessage();
        try{
            IotSdkClient.deleteDevice(deviceName);
            rm.setSuccess(true);
            return rm;
        }catch (Exception e){
            rm.setSuccess(false);
            return rm;
        }
    }
    @PostMapping("/querydevice")
    public ReturnMessage queryDevice() {
        ReturnMessage rm = new ReturnMessage();
        try{
            List<Device> list = new ArrayList<>();
            //将调用API  IotSdkClient.queryDevice() 返回的数据转成JSONObject方便获取值
            JSONObject jsonallmsg = JSONObject.parseObject(IotSdkClient.queryDevice());
            JSONObject jsonbodymsg = JSONObject.parseObject(jsonallmsg.get("body").toString());
            JSONObject jsondata1 = JSONObject.parseObject(jsonbodymsg.get("data").toString());
            JSONArray jsondata2 = JSONObject.parseArray(jsondata1.get("deviceInfo").toString());
            for (Object obj:jsondata2){
                JSONObject jo = JSONObject.parseObject(obj.toString());
                System.out.println(jo);
                Device dev = new Device();
                dev.setDeviceName(jo.get("deviceName").toString());
                if(jo.get("nickname")== null || jo.get("nickname").equals("")){
                    dev.setDeviceNick("");
                }else{
                    dev.setDeviceNick(jo.get("nickname").toString());
                }
                dev.setDeviceStatus(jo.get("deviceStatus").toString());
                list.add(dev);
            }
            rm.setSuccess(true);
            rm.setData(list);
            return rm;
        }catch (Exception e){
            e.printStackTrace();
            rm.setSuccess(false);
            return rm;
        }

    }
    @PostMapping("/startdevice")
    public ReturnMessage startDevice(String deviceName){
        ReturnMessage rm = new ReturnMessage();
        try{
            IotSdkClient.startdevice(deviceName);
            rm.setSuccess(true);
            return rm;
        }catch (Exception e){
            rm.setSuccess(false);
            return rm;
        }
    }
    @PostMapping("/closedevice")
    public ReturnMessage closeDevice(String deviceName){
        ReturnMessage rm = new ReturnMessage();
        try{
            IotSdkClient.closedevice(deviceName);
            rm.setSuccess(true);
            return rm;
        }catch (Exception e){
            rm.setSuccess(false);
            return rm;
        }
    }

}
